package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.AddBrandDTO;
import com.nnnu.wsnackshop.pojo.dto.BrandPageDTO;
import com.nnnu.wsnackshop.pojo.dto.UpdateBrandDTO;
import com.nnnu.wsnackshop.pojo.vo.BrandVO;
import com.nnnu.wsnackshop.result.PageResult;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IProductBrandsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("adminProductBrandsController")
@RequestMapping("/api/admin/product-brands")
@Tag(name = "管理端-商品品牌接口")
@Slf4j
@Validated
@RequiredArgsConstructor
public class ProductBrandsController {

    private final IProductBrandsService brandService;

    @Operation(summary = "分页查询品牌")
    @GetMapping("/page")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("brand:read")
    public Result<PageResult<BrandVO>> pageBrands(@Valid @ParameterObject BrandPageDTO dto) {
        log.info("分页查询品牌：{}", dto);
        return Result.success(brandService.pageBrands(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "新增品牌")
    @PostMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("brand:create")
    @CacheEvict(cacheNames = {"userBrandPages", "userBrandAll", "userBrandDetail"}, allEntries = true)
    public Result<Void> addBrand(@Valid @RequestBody AddBrandDTO dto) {
        log.info("新增品牌：{}", dto);
        brandService.addBrand(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "更新品牌")
    @PutMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("brand:update")
    @CacheEvict(cacheNames = {"userBrandPages", "userBrandAll", "userBrandDetail"}, allEntries = true)
    public Result<Void> updateBrand(@Valid @RequestBody UpdateBrandDTO dto) {
        log.info("更新品牌：{}", dto);
        brandService.updateBrand(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "批量删除品牌")
    @DeleteMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("brand:delete")
    @CacheEvict(cacheNames = {"userBrandPages", "userBrandAll", "userBrandDetail"}, allEntries = true)
    public Result<Void> deleteBrands(
            @RequestParam @Parameter(description = "品牌ID列表", in = ParameterIn.QUERY) List<Integer> ids) {
        log.info("批量删除品牌：{}", ids);
        brandService.deleteBrands(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "修改品牌状态")
    @PutMapping("/{id}/status/{status}")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("brand:update")
    @CacheEvict(cacheNames = {"userBrandPages", "userBrandAll", "userBrandDetail"}, allEntries = true)
    public Result<Void> updateStatus(
            @PathVariable @Parameter(description = "品牌ID", in = ParameterIn.PATH) Integer id,
            @PathVariable @Parameter(description = "状态 1启用 0禁用",in = ParameterIn.PATH) Integer status) {
        log.info("更新品牌状态：id={}, status={}", id, status);
        brandService.updateStatus(id, status);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "品牌详情")
    @GetMapping("/{id}")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("brand:read")
    public Result<BrandVO> getBrandDetail(
            @PathVariable @Parameter(description = "品牌ID", in = ParameterIn.PATH) Integer id) {
        log.info("查询品牌详情：{}", id);
        return Result.success(brandService.getBrandDetail(id), MessageConstants.SELECT_OK);
    }
}
